Java实现B树

本文由作者原创,利用Java实现了B树数据结构,虽然每个节点仅包含key,但详细介绍了实现过程,耗时周末及工作日晚上,现分享在csdn上供读者参考。
摘要由CSDN通过智能技术生成

水平不高,数据结构小学生,java实现B树,纯原创,为了省事,每一个节点只有key,没有写value属性,周末两天在家全部时间+两个工作日的晚上,兴奋之余发布在csdn上,显摆一下,哈哈

package tree;

public class BTree {

    public static final int M = 3;
    @SuppressWarnings("unused")
    private static final int NODES_MIN_LENGTH = M % 2 == 0 ? M / 2 - 1 : M / 2;
    protected BTree parent;
    protected int count;
    protected Node[] nodes = new Node[M + 1];

    public static class Node {
        public int key = Integer.MAX_VALUE;
        public BTree child;

        public Node(int key, BTree child) {
            super();
            this.key = key;
            this.child = child;
        }

        public Node() {
            super();
        }

    }

    public static BTree initBTree() {
        BTree btree = new BTree();
        for (int i = 0; i < btree.nodes.length; i++) {
   
            btree.nodes[i] = new Node();
        }
        return btree;
    }

    public static class Result {
        public BTree node;
        public boolean find;
        public int position;

        public Result(BTree node, boolean find, int position) {
            super();
            this.node = node;
            this.find = find;
            this.position = position;
        }

        public Result() {
            super();
        }
    }

    public static Result nodeSearch(BTree node, int key) {
        Result result = new Result();
        result.node = node;
        int high = node.count;
        int low = 1;
        int mid = (low + high) / 2;
        while (true) {
            if (node.nodes[mid].key == key) {
                result.find = true;
                result.position = mid;
                return result;
            }
            if (node.nodes[mid].key > key) {
                high = mid;
                mid = (low + high) / 2;
            } else {
                low = mid;
                mid = (low + high) / 2;
            }
            if (mid == low) {
                if (node.nodes[mid].key == key) {
                    result.find = true;
                    result.position = mid;
                    return result;
                }
                if (low == high) {
                    if (node.nodes[mid].key < key) {
                        result.find = false;
                        result.position = mid;
                        return result;
                    } else {
                        result.find = false;
                        result.position = mid - 1;
                        return result;
                    }

                }
                if (node.nodes[mid + 1].key == key) {
                    result.find = true;
                    result.position = mid + 1;
                    return result;
                }
                if (node.nodes[mid].key < key && node.nodes[mid + 1].key
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值